package com.jyw.common.exception;

import com.jyw.common.constant.HttpStatus;

import java.util.function.BooleanSupplier;

/**
 * @author 沈峻
 * @ClassName ValidationException
 * @Description 验证异常
 * @Date 2023/9/13 16:19
 **/
public class ValidationException extends RuntimeException{

    private static final long serialVersionUID = 1L;

    /**
     * 错误码
     */
    private Integer code;

    /**
     * 错误提示
     */
    private String message;

    /**
     * 错误明细，内部调试错误
     *
     */
    private String detailMessage;


    /**
     * 空构造方法，避免反序列化问题
     */
    public ValidationException()
    {
    }

    public ValidationException(String message)
    {
        this.message = message;
        this.code = HttpStatus.NOT_VALIDATE;
    }

    public ValidationException(String message, Integer code)
    {
        this.message = message;
        this.code = code;
    }

    public String getDetailMessage()
    {
        return detailMessage;
    }

    @Override
    public String getMessage()
    {
        return message;
    }

    public Integer getCode()
    {
        return code;
    }

    public ValidationException setMessage(String message)
    {
        this.message = message;
        return this;
    }

    public ValidationException setDetailMessage(String detailMessage)
    {
        this.detailMessage = detailMessage;
        return this;
    }


    public static void assertThat(BooleanSupplier func, String message) {
        try {
            boolean condition = func.getAsBoolean();
            if (!condition) {
                throw new ValidationException(message);
            }
        } catch (Exception e) {
            throw new ValidationException(message);
        }
    }
}
